package cn.easybuy.servlet.users;

import cn.easybuy.entity.User;
import cn.easybuy.service.UserService;
import cn.easybuy.service.impl.UserServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/userUpdServlet")
public class UserUpdServlet  extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            resp.setContentType("text/html;charset=utf-8");
            req.setCharacterEncoding("utf-8");
            int id =Integer.parseInt(req.getParameter("id")) ;
            String loginName = req.getParameter("loginName");
            String userName = req.getParameter("userName");
             int sex =Integer.parseInt(req.getParameter("sex")) ;
            String password = req.getParameter("password");
            String identityCode = req.getParameter("identityCode");
            String email = req.getParameter("email");
            String mobile = req.getParameter("mobile");
            int type =Integer.parseInt(req.getParameter("type")) ;
            User u = new User(id,loginName,userName,sex,password,identityCode,email,mobile,type);
            boolean b  = userService.updUsers(u);
            if (b){
                req.setAttribute("msg","修改成功");
            }
            else{
                req.setAttribute("msg","修改失败");
            }
            RequestDispatcher requestDispatcher = req.getRequestDispatcher("/userPageServlet?pageNo=1");
            requestDispatcher.forward(req,resp);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
